import { saveLightInfor, saveGroupSetInfor, searchAllLightOfThisArea, saveStateAreaSetInfor, saveAreaInfor } from './homeUtils'
import { isRGBDeviceset, isDimDeviceset, allAreaGetTempRange, allSubgroupGetTempRange, getTempRange } from './pidUtils'
import { getOctets2Msg } from './dataUtils'
import { homeDeng } from "../api/index"
import PubSub from 'pubsub-js'
import { meshLog, SPRemove, SPGet, SPSet } from '../config'
import { isLightDevice } from './pidUtils';
//添加新的属性的时候来修改数据结构的时候只需要修改这个地方的数据结构就好了
export const createNewDateThree = (mAreaSet, mGroupSet, mDeviceSet) => {
    //从新组建一个新的areaSet
    let areaSetx = []
    mAreaSet.map((item, key) => {
        areaSetx[key] = {
            name: item.name, areaNo: item.areaNo, isOnoff: '00', dim: '19661', tem: '2700',
            minTrim: item.minTrim, maxTrim: item.maxTrim, isrgb: '0', rgbH: '0', rgbS: '0', rgbL: '0',
            isOnline: '00', isdimdeng: '00'
        }
    })
    // meshLog("adminUtils.js 19 新建之后放在localStorage的areaSetx", areaSetx);
    SPSet('areaSetx', JSON.stringify(areaSetx))//存到localStorage
    //从新组建一个新的groupSet
    let groupSetx = []
    mGroupSet.map((item, key) => {
        groupSetx[key] = {
            name: item.name, areaNo: item.areaNo, groupNo: item.groupNo,
            minTrim: item.minTrim, maxTrim: item.maxTrim, isOnoff: '00', dim: '19661', tem: '2700',
            isrgb: '0', rgbH: '0', rgbS: '0', rgbL: '0', isOnline: '00', isdimdeng: '00'
        }
    })
    //meshLog("adminUtils.js,30 新建之后放在localStorage的groupSetx", groupSetx);
    SPSet('groupSetx', JSON.stringify(groupSetx))//存到localStorage
    //从新组建一个新的deviceSet
    let deviceSetx = []
    mDeviceSet.map((item, key) => {
        let isrgb = isRGBDeviceset(item.pid)
        let isdimdeng = isDimDeviceset(item.pid)
        deviceSetx[key] = {
            address: item.address, areaNo: item.areaNo, mac: item.mac,
            maxPower: item.maxPower, maxTrim: item.maxTrim,
            minTrim: item.minTrim, pid: item.pid, powerIndex: item.powerIndex, groupNo: item.groupNo,
            tempIndex: item.tempIndex, vid: item.vid, isOnoff: '00', dim: '19661', tem: '2700',
            isrgb: isrgb, rgbH: '0', rgbS: '0', rgbL: '0', isOnline: '00', isdimdeng: isdimdeng, 'lightID': item.lightID
        }
    })
    // meshLog("adminUtils.js 45,新建之后放在localStorage的deviceSetx", deviceSetx);
    SPSet('deviceSetx', JSON.stringify(deviceSetx))//存到localStorage
}
//修改deviceSetx的数据结构，主要是为了mqtt单发获取灯的数据来
export const mqttdeviceset = () => {
    let deviceSetx = JSON.parse(SPGet("deviceSetx"));
    let devisUnOnline = deviceSetx.filter((item) => {
        if ((item.isOnline == '00') && isLightDevice(item.pid)) {
            return item
        }
    })
    meshLog('adminUtils.js,57行 原本devisUnOnline', devisUnOnline);
    let areaSetx = JSON.parse(SPGet("areaSetx"));
    let devmqttdate = []
    let zhonghe = []
    if (areaSetx.length > 0) {
        meshLog('adminUtils.js,61行 有组信息，需要通过组来判断',);
        for (let i = 0; i < areaSetx.length; i++) {
            devmqttdate[i] = searchAllLightOfThisArea(areaSetx[i].areaNo)
        }
        meshLog('adminUtils.jsx,67行 每个组中的灯', devmqttdate);
        let mqttUngroupdate = searchAllLightOfThisArea('C000')
        meshLog('adminUtils.jsx,67行 所有Ungroup的灯', mqttUngroupdate);
        let fengdate = []
        let zhonghedate1 = []
        let zhonghedate2 = []
        for (let b = 0; b < devmqttdate.length; b++) {
            fengdate[(2) * b] = devmqttdate[b].slice(0, Math.round(devmqttdate[b].length) * 0.3);
            zhonghedate1 = zhonghedate1.concat(fengdate[(2) * b])
            fengdate[(2) * b + 1] = devmqttdate[b].slice(Math.round(devmqttdate[b].length) * 0.3, (devmqttdate[b].length));
            zhonghedate2 = zhonghedate2.concat(fengdate[(2) * b + 1])
        }
        zhonghe = zhonghedate1.concat(mqttUngroupdate, zhonghedate2)
        // meshLog('zhonghedate1', zhonghedate1);
        // meshLog('zhonghedate1', zhonghedate2);
        meshLog('adminUtils.js 80 ,整合之后所有的灯', zhonghe);
    } else {
        zhonghe = deviceSetx.filter((item) => {
            if ((item.isOnline == '00') && isLightDevice(item.pid)) {
                return item
            }
        })
    }

    return zhonghe
}
//方法解释 HSL 转 RGB
export const HSLTORGB = (rgbH, rgbS, rgbL) => {
    let h = rgbH
    let s = rgbS
    let l = rgbL
    let r = 0;
    let g = 0;
    let b = 0;
    let RGB = []
    //hsl转RGB
    if (s == 0) {
        r = g = b = l; // achromatic
    } else {
        var hue2rgb = function hue2rgb(p, q, t) {
            if (t < 0) t += 1;
            if (t > 1) t -= 1;
            if (t < 1 / 6) return p + (q - p) * 6 * t;
            if (t < 1 / 2) return q;
            if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
            return p;
        }
        var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
        var p = 2 * l - q;
        r = hue2rgb(p, q, h + 1 / 3);
        g = hue2rgb(p, q, h);
        b = hue2rgb(p, q, h - 1 / 3);
    }
    //hsl转RGB结束
    RGB[0] = r; RGB[1] = g; RGB[2] = b
    return RGB
}
//方法解释 RGB 转 hsv
export const RGBTOHSV = (r, g, b) => {
    //RGB转hsv
    let HSV = []
    let R = r
    let G = g
    let B = b
    const max = Math.max(R, G, B)
    const min = Math.min(R, G, B)
    const range = max - min
    let V = max
    let S = V === 0 ? 0 : range / V
    let H = 0
    if (R === V) H = (60 * (G - B)) / range
    if (G === V) H = 120 + (60 * (B - R)) / range
    if (B === V) H = 240 + (60 * (R - G)) / range
    if (range === 0) H = 0
    if (H < 0) H += 360
    H = (H)
    S = S
    V = V
    //RGB转hsv结束
    HSV[0] = H; HSV[1] = S; HSV[2] = V
    return HSV
}
//方法解释 RGB 转 6位颜色的
export const RGBTOSixColor = (data) => {
    let rgb = data
    let color = '#' + ((1 << 24) + (parseInt(rgb[0] * 255) << 16) + (parseInt(rgb[1] * 255) << 8) + parseInt(rgb[2] * 255)).toString(16).slice(1)
    return color
}
//方法解释 hsv转rgb
export const HSVTORGB = (hhh, RR, dimx) => {
    //meshLog('hhh', hhh);
    let RGB = []
    let R = 0;
    let G = 0;
    let B = 0;
    let sss = (Math.sqrt(RR)) / 125
    let vvv = dimx / 100
    // let vvv = 1
    // let sss = 1
    // meshLog('sss', sss);
    // meshLog('vvv', vvv);
    let i = parseInt(((hhh) / 60) % 6)
    let f = hhh / 60 - i
    let p = vvv * (1 - sss)
    let q = vvv * (1 - f * sss)
    let t = vvv * (1 - (1 - f) * sss)
    switch (i) {
        case 0:
            R = vvv; G = t; B = p
            break;
        case 1:
            R = q; G = vvv; B = p
            break;
        case 2:
            R = p; G = vvv; B = t
            break;
        case 3:
            R = p; G = q; B = vvv
            break;
        case 4:
            R = t; G = p; B = vvv
            break;
        case 5:
            R = vvv; G = p; B = q
            break;
        default:
            break;
    }
    //hsv转rgb结束
    SPSet('hhh', JSON.stringify(hhh))//存到localStorage
    SPSet('sss', JSON.stringify(sss))//存到localStorage
    RGB[0] = R; RGB[1] = G; RGB[2] = B
    return RGB
}
//方法解释 hsv转rgb.这个传的值是直接可以用的HSV，不需要转换
export const HSVtoRGB = (hhh, sss, vvv) => {
    let RGB = []
    let R = 0;
    let G = 0;
    let B = 0;
    // let vvv = 1
    // let sss = 1
    // meshLog('sss', sss);
    // meshLog('vvv', vvv);
    let i = parseInt(((hhh) / 60) % 6)
    let f = hhh / 60 - i
    let p = vvv * (1 - sss)
    let q = vvv * (1 - f * sss)
    let t = vvv * (1 - (1 - f) * sss)
    switch (i) {
        case 0:
            R = vvv; G = t; B = p
            break;
        case 1:
            R = q; G = vvv; B = p
            break;
        case 2:
            R = p; G = vvv; B = t
            break;
        case 3:
            R = p; G = q; B = vvv
            break;
        case 4:
            R = t; G = p; B = vvv
            break;
        case 5:
            R = vvv; G = p; B = q
            break;
        default:
            break;
    }
    //hsv转rgb结束
    RGB[0] = R; RGB[1] = G; RGB[2] = B
    return RGB
}
//方法解释 //rgb转hsl
export const RGBTOHSL = (R, G, B) => {
    //rgb转hsl
    let hsl = []
    let hslR = R / 255
    let hslG = G / 255
    let hslB = B / 255;
    let max = Math.max(hslR, hslG, hslB)
    let min = Math.min(hslR, hslG, hslB);
    let hslh, hsls, hsll = (max + min) / 2;
    if (max == min) {
        hslh = hsls = 0; // achromatic
    } else {
        var d = max - min;
        hsls = hsll > 0.5 ? d / (2 - max - min) : d / (max + min);
        switch (max) {
            case hslR: hslh = (hslG - hslB) / d + (hslG < hslB ? 6 : 0); break;
            case hslG: hslh = (hslB - hslR) / d + 2; break;
            case hslB: hslh = (hslR - hslG) / d + 4; break;
        }
        hslh /= 6;
    }
    //rgb转hsl
    SPSet('hslh', JSON.stringify(hslh))//存到localStorage
    SPSet('hsls', JSON.stringify(hsls))//存到localStorage
    SPSet('hsll', JSON.stringify(hsll))//存到localStorage
    hsl[0] = hslh; hsl[1] = hsls; hsl[2] = hsll
    return hsl
}
//mqtt数据返回的处理方法
export const mqtthHndleDate = (devSetItem) => {
    //修改数据库发一系列操作些列方法
    saveLightInfor(devSetItem)
    //修改localStorage上groupSet的数据，
    //因为数据值是一样的，dev属性参数也都有，所有可以直接用保存单灯的数据来直接修改group的数据
    saveGroupSetInfor(devSetItem)
    //修改localStorage上groupSet的数据 结束
    //通过上面找到的单灯来知道这个单灯所在的组，在通过组来找到这个组里的所有灯，修改单灯有在组里的信息
    if (devSetItem[0].areaNo == 'C000' || devSetItem[0].areaNo == '0') {
        meshLog('adminUtils.js,279行,这个灯没有组');
    } else {
        //修改areaSet开关的数据
        let areaa = devSetItem[0].areaNo
        let devicearea = [];//属于这个组的全部灯和subgroup灯的集合
        let areaSetxxx = [];
        devicearea = searchAllLightOfThisArea(areaa)
        // meshLog("所在的areaNo所有灯的信息包括subgroup", devicearea);
        let deviceareaisOnoff = devicearea.map((item, key) => {
            return (
                item.isOnoff
            )
        })
        let a = deviceareaisOnoff.find(item => item == '01');
        let aaa = ''
        if (a == '01') {
            aaa = '01'
        } else {
            aaa = '00'
        }
        const areaSetxx = JSON.parse(SPGet("areaSetx"))
        areaSetxxx = saveStateAreaSetInfor(areaSetxx, aaa, areaa);//修改areaset的方法
        SPSet('areaSetx', JSON.stringify(areaSetxxx))//从新上传到loaclstorage
        //修改areaSet开关的数据结束
        //修改组中isonline的数据
        let deviceareaisonline = devicearea.map((item, key) => {
            return (
                item.isOnline
            )
        })
        let b = deviceareaisonline.find(item => item == '01');
        let isOnlineA = ''
        if (b == '01') {
            isOnlineA = '01'
        } else {
            isOnlineA = '00'
        }
        //修改组中isonline的数据结束
        //修改area中RGB有关的数据
        //第一步修改组中isrgb的状态(这个可以放在home界面来做)
        let deviceareaisrgb = devicearea.map((item, key) => {
            return (
                item.isrgb
            )
        })
        let c = deviceareaisrgb.find(item => item == '01');
        let isrgbA = ''
        if (c == '01') {
            isrgbA = '01'
        } else {
            isrgbA = '00'
        }
        //第二步修改组中rgb的三个值
        let rgbinfor = devicearea.find(item => item.isrgb == '01' && item.isOnoff == '01' && item.rgbH > 0)
        //meshLog('rgbinfor', rgbinfor);
        let rgbH = '0'
        let rgbS = '0'
        let rgbL = '0'
        if (rgbinfor === undefined) {
            //meshLog('devicearea', devicearea);
            meshLog('adminUtils.js,339行,这个组中没有rgb灯');
        } else {
            rgbH = rgbinfor.rgbH
            rgbS = rgbinfor.rgbS
            rgbL = rgbinfor.rgbL
        }
        //修改area中RGB有关的数据结束
        //修改组中isdimdeng的数据
        let deviceareaisdimdeng = devicearea.map((item, key) => {
            return (
                item.isdimdeng
            )
        })
        let d = deviceareaisdimdeng.find(item => item === '00');
        let isdimdengA = ''
        if (d == '00') {
            isdimdengA = '00'
        } else {
            isdimdengA = '01'
        }
        //修改组中isdimdeng的数据结束
        //将上面所有的数据整合在一起统一处理,修改localStorage上areaSetx的数据
        const areaSetx = JSON.parse(SPGet("areaSetx"))
        let areaitem;
        for (let index = 0; index < areaSetx.length; index++) {
            if (areaSetx[index].areaNo == areaa) {
                areaitem = areaSetx[index]
            }
        }
        // meshLog('灯对应的需要修改组的信息', areaitem);
        areaitem.isOnline = isOnlineA
        areaitem.isdimdeng = isdimdengA
        areaitem.isrgb = isrgbA
        areaitem.rgbH = rgbH
        areaitem.rgbS = rgbS
        areaitem.rgbL = rgbL
        //meshLog('areaitem', areaitem);
        saveAreaInfor(areaitem)
    }
}
//首次加载需要通过rgbpandun来确定点击的是组还是单灯还是subgroup
export const FirstPandanRGB = (rgbpanduan) => {
    let rgbshuju = JSON.parse(SPGet("rgbshuju"))
    //meshLog('rgbshuju', rgbshuju);
    let AllRGBdate = []
    let areaitem;
    let areaitempandun;
    if (rgbpanduan == 1) {
        let areaSetx = JSON.parse(SPGet("areaSetx"));
        for (let index = 0; index < areaSetx.length; index++) {
            if (areaSetx[index].areaNo == rgbshuju.areaNo) {
                areaitem = areaSetx[index]
            }
        }
        areaitempandun = areaitem.areaNo
    } else if (rgbpanduan == 2) {
        let areaSetx = JSON.parse(SPGet("deviceSetx"));
        for (let index = 0; index < areaSetx.length; index++) {
            if (areaSetx[index].address == rgbshuju.address) {
                areaitem = areaSetx[index]
            }
        }
        meshLog('adminUtils.js 401 areaitem', areaitem);
        areaitempandun = areaitem.address
    } else if (rgbpanduan == 3) {
        let areaSetx = JSON.parse(SPGet("groupSetx"));
        for (let index = 0; index < areaSetx.length; index++) {
            if (areaSetx[index].groupNo == rgbshuju.groupNo) {
                areaitem = areaSetx[index]
            }
        }
        areaitempandun = areaitem.groupNo
    }
    //如果是cct到rgb界面这边需要加判断，也就是说cct状态下rgb没有值要赋值给hsl
    if (areaitem.rgbH + areaitem.rgbS + areaitem.rgbL == 0) {
        SPSet('hslh', JSON.stringify(0.5))//存到localStorage
        SPSet('hsls', JSON.stringify(0.5))//存到localStorage
        SPSet('hsll', JSON.stringify(0.5))//存到localStorage
        meshLog('adminUtils.js,417行,重新赋值的');
    } else {
        SPSet('hslh', JSON.stringify(areaitem.rgbH))//存到localStorage
        SPSet('hsls', JSON.stringify(areaitem.rgbS))//存到localStorage
        SPSet('hsll', JSON.stringify(areaitem.rgbL))//存到localStorage
    }
    PubSub.publish('RGB', 1)
    //meshLog(areaitem);
    AllRGBdate[0] = areaitem;
    AllRGBdate[1] = areaitempandun
    return AllRGBdate
}
export const PandanRGB = (rgbpanduan) => {
    let rgbshuju = JSON.parse(SPGet("rgbshuju"))
    let AllRGBdate = []
    let areaitem;
    let areaitempandun;
    if (rgbpanduan == 1) {
        let areaSetx = JSON.parse(SPGet("areaSetx"));
        for (let index = 0; index < areaSetx.length; index++) {
            if (areaSetx[index].areaNo == rgbshuju.areaNo) {
                areaitem = areaSetx[index]
            }
        }
        areaitempandun = areaitem.areaNo
    } else if (rgbpanduan == 2) {
        let areaSetx = JSON.parse(SPGet("deviceSetx"));
        for (let index = 0; index < areaSetx.length; index++) {
            if (areaSetx[index].address == rgbshuju.address) {
                areaitem = areaSetx[index]
            }
        }
        areaitempandun = areaitem.address
    } else if (rgbpanduan == 3) {
        let areaSetx = JSON.parse(SPGet("groupSetx"));
        for (let index = 0; index < areaSetx.length; index++) {
            if (areaSetx[index].groupNo == rgbshuju.groupNo) {
                areaitem = areaSetx[index]
            }
        }
        areaitempandun = areaitem.groupNo
    }
    // SPSet('hslh', JSON.stringify(areaitem.rgbH))//存到localStorage
    // SPSet('hsls', JSON.stringify(areaitem.rgbS))//存到localStorage
    // SPSet('hsll', JSON.stringify(areaitem.rgbL))//存到localStorage
    //meshLog(areaitem);
    AllRGBdate[0] = areaitem;
    AllRGBdate[1] = areaitempandun
    return AllRGBdate
}
//连贯方法，hls-rgb-hsv-rgb-hsl，在home界面改亮度的时候，需要改hsl的值
//思路就是先获取原本的hsl转到HSV，然后不要V的值，用亮度是值替换v然后在转成hsl
export const HSLtoHSVtoHSL = (hslh, hsls, hsll, dim) => {
    let RGB = HSLTORGB(hslh, hsls, hsll)
    let r = RGB[0]
    let g = RGB[1]
    let b = RGB[2]
    let HSV = RGBTOHSV(r, g, b)
    //miecuo
    let h = HSV[0]
    let s = HSV[1]
    let v = dim / 65535
    let RGB2 = HSVtoRGB(h, s, v)
    let r2 = RGB2[0] * 255
    let g2 = RGB2[1] * 255
    let b2 = RGB2[2] * 255
    let HSL = RGBTOHSL(r2, g2, b2)
    return HSL
}
//settingColorRGB上面颜色图片界面的数据修改部分 async
export const SetRGBdate = async (R, G, B) => {
    let rgbpanduan = JSON.parse(SPGet("rgbpanduan"))
    let areaSetxxx = PandanRGB(rgbpanduan)
    let areaSetx = JSON.parse(SPGet("areaSetx"));
    //rgb转hsl
    let hsl = RGBTOHSL(R, G, B)
    let hslh = hsl[0]
    let hsls = hsl[1]
    let hsll = hsl[2]
    //rgb转hsl
    SPSet('hslh', JSON.stringify(hslh))//存到localStorage
    SPSet('hsls', JSON.stringify(hsls))//存到localStorage
    SPSet('hsll', JSON.stringify(hsll))//存到localStorage
    //转换过程
    let value1 = Math.ceil(hslh * 65535);
    let value2 = Math.ceil(hsls * 65535);
    let value3 = Math.ceil(hsll * 65535);
    const token = SPGet('token')
    const email = SPGet('email')
    const meshUUID = SPGet('meshUUID')
    const addrs = areaSetxxx[1].substring(2, 4) + areaSetxxx[1].substring(0, 2);
    const opCode = "8277";
    const value = getOctets2Msg(value3) + getOctets2Msg(value1) + getOctets2Msg(value2);
    //meshLog(token, email, meshUUID, addrs, opCode, value);
    const result = await homeDeng(token, email, meshUUID, addrs, opCode, value);
    //meshLog(result);
    const { ctl_status } = result;
    if (ctl_status == 0) {
        if (rgbpanduan == 1) {
            //修改localStorage上areaSetx的数据
            let areaitem;
            for (let index = 0; index < areaSetx.length; index++) {
                if (areaSetx[index].areaNo == areaSetxxx[1]) {
                    areaitem = areaSetx[index]
                }
            }
            areaitem.rgbH = hslh
            areaitem.rgbS = hsls
            areaitem.rgbL = hsll
            //meshLog('areaitem', areaitem);
            saveAreaInfor(areaitem)
            //修改localStorage上deviceSetx的数据
            let deviceSetx = JSON.parse(SPGet("deviceSetx"));
            let devSetItem = [];
            //查询属于这个areaNo的 device
            devSetItem = deviceSetx.filter((item) => {
                if (item.areaNo == areaSetxxx[1] && item.isrgb == '01' && item.isOnline == '01') {
                    item.rgbH = hslh
                    item.rgbS = hsls
                    item.rgbL = hsll
                    return item
                }
            })
            meshLog('adminUtils.js,540行,devSetItem', devSetItem);
            saveLightInfor(devSetItem)
            //查询属于这个areaNo的 subgroup
            let groupSetx = JSON.parse(SPGet("groupSetx"));
            let groupSetItem = [];
            groupSetItem = groupSetx.filter((item) => {
                if (item.areaNo == areaSetxxx[1] && item.isrgb == '01' && item.isOnline == '01') {
                    item.rgbH = hslh
                    item.rgbS = hsls
                    item.rgbL = hsll
                    return item
                }
            })
            //meshLog('groupSetItem', groupSetItem);
            saveGroupSetInfor(groupSetItem)
            // meshLog("传到页面上需要展示右边数据的组", item);
            PubSub.publish('data', areaitem)
            //修改亮度的背景条颜色
            PubSub.publish('backgroudCololArea', areaitem)
            //修改色温条的球位置的修改色温背景色
            PubSub.publish('qiuArea', areaitem)
        } else if (rgbpanduan == 2) {
            let deviceSetx = JSON.parse(SPGet("deviceSetx"));
            let devSetItem = [];
            //查询属于这个areaNo的 device
            devSetItem = deviceSetx.filter((item) => {
                if (item.address == areaSetxxx[1] && item.isOnline == '01') {
                    item.rgbH = hslh
                    item.rgbS = hsls
                    item.rgbL = hsll
                    return item
                }
            })
            //meshLog('adminUtils.js,530行,devSetItem', devSetItem);
            saveLightInfor(devSetItem)
            //修改单灯的rgb的rgbH需要检查所有在这个组里的这个值，如果全部一样就要修改这个组的rgbH的值
            setTimeout(() => {
                PubSub.publish('rgbHneifanhui', hslh)
            }, 500)
            //修改亮度的背景条颜色
            if (devSetItem[0].areaNo == 'C000' || devSetItem[0].areaNo == '0' && devSetItem[0].isOnline == '01') {
                meshLog('adminUtils.js,582行,灯在ungroup');
                PubSub.publish('ungrouprgbzhuangtai', devSetItem[0])//修改ungroup背景和数据显示
            } else {
                meshLog('adminUtils.js,585行,灯是1');
                PubSub.publish('LightbackgroudColol', devSetItem)
                PubSub.publish('data', devSetItem[0])//这个是从新获取组内的灯的信息
            }
            PubSub.publish('RGBrgbHneifanhui', 1)//判断色温是不是一样的，要是一样要返回给组
        } else if (rgbpanduan == 3) {
            let groupSetx = JSON.parse(SPGet("groupSetx"));
            let groupSetItem = [];
            groupSetItem = groupSetx.filter((item) => {
                if (item.groupNo == areaSetxxx[1] && item.isOnline == '01') {
                    item.rgbH = hslh
                    item.rgbS = hsls
                    item.rgbL = hsll
                    return item
                }
            })
            //meshLog('groupSetItem', groupSetItem);
            saveGroupSetInfor(groupSetItem)
            //修改localStorage上deviceSetx的数据
            let deviceSetx = JSON.parse(SPGet("deviceSetx"));
            let devSetItem = [];
            //查询属于这个areaNo的 device
            devSetItem = deviceSetx.filter((item) => {
                if (item.groupNo == areaSetxxx[1] && item.isOnline == '01') {
                    item.rgbH = hslh
                    item.rgbS = hsls
                    item.rgbL = hsll
                    return item
                }
            })
            //meshLog('devSetItem', devSetItem);
            saveLightInfor(devSetItem)
            setTimeout(() => {
                PubSub.publish('rgbHneifanhui', hslh)
            }, 500)
            //修改亮度的背景条颜色
            PubSub.publish('SubLightbackgroudColol', devSetItem)
            PubSub.publish('data', devSetItem[0])//这个是从新获取组内的灯的信息
            PubSub.publish('RGBrgbHneifanhui', 1)//判断色温是不是一样的，要是一样要返回给组（主要是针对单灯的时候）
        }
    }
    //反馈给hsl页面
    PubSub.publish('HSL1', 1)
    PubSub.publish('HSL2', 1)
    PubSub.publish('HSL3', 1)
}
//RGB界面中的hsl界面的数据修改部分 async
export const SetHSLdate = async (hslh, hsls, hsll) => {
    let rgbpanduan = JSON.parse(SPGet("rgbpanduan"))
    let areaSetxxx = PandanRGB(rgbpanduan)
    let areaSetx = JSON.parse(SPGet("areaSetx"));
    const token = SPGet('token')
    const email = SPGet('email')
    const meshUUID = SPGet('meshUUID')
    const addrs = areaSetxxx[1].substring(2, 4) + areaSetxxx[1].substring(0, 2);
    const opCode = "8277";
    let value1 = Math.ceil(hslh * 65535);
    let value2 = Math.ceil(hsls * 65535);
    let value3 = Math.ceil(hsll * 65535);
    const value = getOctets2Msg(value3) + getOctets2Msg(value1) + getOctets2Msg(value2);
    //meshLog(token, email, meshUUID, addrs, opCode, value);
    let result = await homeDeng(token, email, meshUUID, addrs, opCode, value);
    //meshLog(result);
    const { ctl_status } = result;
    if (ctl_status == 0) {
        if (rgbpanduan == 1) {
            //修改localStorage上areaSetx的数据
            let areaitem;
            for (let index = 0; index < areaSetx.length; index++) {
                if (areaSetx[index].areaNo == areaSetxxx[1]) {
                    areaitem = areaSetx[index]
                }
            }
            areaitem.rgbH = hslh
            areaitem.rgbS = hsls
            areaitem.rgbL = hsll
            //meshLog('areaitem', areaitem);
            saveAreaInfor(areaitem)
            //修改localStorage上deviceSetx的数据
            let deviceSetx = JSON.parse(SPGet("deviceSetx"));
            let devSetItem = [];
            //查询属于这个areaNo的 device
            devSetItem = deviceSetx.filter((item) => {
                if (item.areaNo == areaSetxxx[1] && item.isrgb == '01' && item.isOnline == '01') {
                    item.rgbH = hslh
                    item.rgbS = hsls
                    item.rgbL = hsll
                    return item
                }
            })
            //('adminUtils.js,631行,areaSetxxx[1]', areaSetxxx[1]);
            //meshLog('adminUtils.js,632行,devSetItem', devSetItem);
            saveLightInfor(devSetItem)
            //查询属于这个areaNo的 subgroup
            let groupSetx = JSON.parse(SPGet("groupSetx"));
            let groupSetItem = [];
            groupSetItem = groupSetx.filter((item) => {
                if (item.areaNo == areaSetxxx[1] && item.isrgb == '01' && item.isOnline == '01') {
                    item.rgbH = hslh
                    item.rgbS = hsls
                    item.rgbL = hsll
                    return item
                }
            })
            //meshLog('groupSetItem', groupSetItem);
            saveGroupSetInfor(groupSetItem)
            // meshLog("传到页面上需要展示右边数据的组", item);
            PubSub.publish('data', areaitem)
            //修改亮度的背景条颜色
            PubSub.publish('backgroudCololArea', areaitem)
            //修改色温条的球位置的修改色温背景色
            PubSub.publish('qiuArea', areaitem)
        } else if (rgbpanduan == 2) {
            let deviceSetx = JSON.parse(SPGet("deviceSetx"));
            let devSetItem = [];
            //查询属于这个areaNo的 device
            devSetItem = deviceSetx.filter((item) => {
                if (item.address == areaSetxxx[1] && item.isOnline == '01') {
                    item.rgbH = hslh
                    item.rgbS = hsls
                    item.rgbL = hsll
                    return item
                }
            })
            //meshLog('devSetItem', devSetItem);
            saveLightInfor(devSetItem)
            //修改单灯的rgb的rgbH需要检查所有在这个组里的这个值，如果全部一样就要修改这个组的rgbH的值
            setTimeout(() => {
                PubSub.publish('rgbHneifanhui', hslh)
            }, 500)
            //修改亮度的背景条颜色
            let areadate = PandanRGB(rgbpanduan)
            if (devSetItem[0].areaNo == 'C000' || devSetItem[0].areaNo == '0') {
                meshLog('adminUtils.js 718 灯在ungroup');
                PubSub.publish('ungrouprgbzhuangtai', areadate[0]) //修改ungroup背景和数据显示
            } else {
                PubSub.publish('LightbackgroudColol', devSetItem)
                // meshLog("传到页面上需要展示右边数据的组", item);
                PubSub.publish('data', devSetItem[0])
                PubSub.publish('RGBrgbHneifanhui', 1)//判断色温是不是一样的，要是一样要返回给组
            }
        } else if (rgbpanduan == 3) {
            let groupSetx = JSON.parse(SPGet("groupSetx"));
            let groupSetItem = [];
            groupSetItem = groupSetx.filter((item) => {
                if (item.groupNo == areaSetxxx[1] && item.isOnline == '01') {
                    item.rgbH = hslh
                    item.rgbS = hsls
                    item.rgbL = hsll
                    return item
                }
            })
            //meshLog('groupSetItem', groupSetItem);
            saveGroupSetInfor(groupSetItem)
            //修改localStorage上deviceSetx的数据
            let deviceSetx = JSON.parse(SPGet("deviceSetx"));
            let devSetItem = [];
            //查询属于这个areaNo的 device
            devSetItem = deviceSetx.filter((item) => {
                if (item.groupNo == areaSetxxx[1] && item.isOnline == '01') {
                    item.rgbH = hslh
                    item.rgbS = hsls
                    item.rgbL = hsll
                    return item
                }
            })
            //meshLog('devSetItem', devSetItem);
            saveLightInfor(devSetItem)
            setTimeout(() => {
                PubSub.publish('rgbHneifanhui', hslh)
            }, 500)
            //修改亮度的背景条颜色
            PubSub.publish('SubLightbackgroudColol', devSetItem)
            // meshLog("传到页面上需要展示右边数据的组", item);
            PubSub.publish('data', devSetItem[0])
            PubSub.publish('RGBrgbHneifanhui', 1)//判断色温是不是一样的，要是一样要返回给组
        }
        //主要是hsl条的修改背景色的
        PubSub.publish('HSL2', 1)
        PubSub.publish('HSL3', 1)
    }
}
//RGB界面中的cct界面的数据修改部分 async
export const SetCCTdate = async (temx) => {
    let rgbpanduan = JSON.parse(SPGet("rgbpanduan"))
    let areadate = PandanRGB(rgbpanduan)
    let areaSetx = JSON.parse(SPGet("areaSetx"));
    const values = temx
    const token = SPGet('token')
    const email = SPGet('email')
    const meshUUID = SPGet('meshUUID')
    let addrs
    if (rgbpanduan == 2) {
        const addd2 = parseInt(1 + areadate[1], 16) + 1//16转换成10进制然后在+1
        const addd3 = addd2.toString(16)//10转换成16进制
        const addss3 = addd3.substring(1, 5)
        //改写成大写meshLog("addss3", addss3.toUpperCase());
        const addr4 = addss3.toUpperCase();
        addrs = addr4.substring(2, 4) + addr4.substring(0, 2);
    } else {
        addrs = areadate[1].substring(2, 4) + areadate[1].substring(0, 2);
    }
    const opCode = "8265";
    const value1 = parseInt(values);
    const value = getOctets2Msg(value1);
    let result = await homeDeng(token, email, meshUUID, addrs, opCode, value);
    //meshLog(result);
    const { ctl_status } = result;
    if (ctl_status == 0) {
        if (rgbpanduan == 1) {
            //修改localStorage上areaSetx的数据
            let areaitem;
            for (let index = 0; index < areaSetx.length; index++) {
                if (areaSetx[index].areaNo == areadate[1]) {
                    areaitem = areaSetx[index]
                }
            }
            areaitem.tem = temx
            areaitem.rgbH = 0
            meshLog('adminUtils.js,804行,areaitem', areaitem);
            saveAreaInfor(areaitem)
            //修改localStorage上deviceSetx的数据
            let deviceSetx = JSON.parse(SPGet("deviceSetx"));
            let devSetItem = [];
            //查询属于这个areaNo的 device
            devSetItem = deviceSetx.filter((item) => {
                if (item.areaNo == areadate[1] && item.isOnline == '01') {
                    item.tem = temx
                    item.rgbH = 0
                    return item
                }
            })
            //meshLog('devSetItem', devSetItem);
            saveLightInfor(devSetItem)
            //查询属于这个areaNo的 subgroup
            let groupSetx = JSON.parse(SPGet("groupSetx"));
            let groupSetItem = [];
            groupSetItem = groupSetx.filter((item) => {
                if (item.areaNo == areadate[1] && item.isOnline == '01') {
                    item.tem = temx
                    item.rgbH = 0
                    return item
                }
            })
            //meshLog('groupSetItem', groupSetItem);
            saveGroupSetInfor(groupSetItem)
            PubSub.publish('data', areadate[0])//返回给home界面
            PubSub.publish('temarea', areadate[1])//返回给area组内
            PubSub.publish('areaCCTbackgroudColol', areadate[0])//返回给area组内
            PubSub.publish('areacctzhuangtai', areadate[0])//返回给组内修改组的背景条和颜色
        } else if (rgbpanduan == 2) {
            let deviceSetx = JSON.parse(SPGet("deviceSetx"));
            let devSetItem = [];
            //查询属于这个device
            devSetItem = deviceSetx.filter((item) => {
                if (item.address == areadate[1]) {
                    item.tem = temx
                    item.rgbH = 0
                    return item
                }
            })
            //meshLog('devSetItem', devSetItem);
            saveLightInfor(devSetItem)
            if (devSetItem[0].areaNo == 'C000' || devSetItem[0].areaNo == '0') {
                meshLog(' adminUtils.js 849 灯在ungroup');
                PubSub.publish('ungroupcctzhuangtai', areadate[0])
            } else {
                PubSub.publish('data', areadate[0])//返回给home界面
                PubSub.publish('temarea', areadate[1])//返回给area组内
            }
            PubSub.publish('temneifanhui', 1)//判断色温是不是一样的，要是一样要返回给组
            PubSub.publish('RGBrgbHneifanhui', 1)//判断色温是不是一样的，要是一样要返回给组，来修改背景颜色和色温背景条
        } else if (rgbpanduan == 3) {
            let deviceSetx = JSON.parse(SPGet("deviceSetx"));
            let devSetItem = [];
            //查询属于这个areaNo的 device
            devSetItem = deviceSetx.filter((item) => {
                if (item.groupNo == areadate[1]) {
                    item.tem = temx
                    item.rgbH = 0
                    return item
                }
            })
            //meshLog('devSetItem', devSetItem);
            saveLightInfor(devSetItem)
            //查询属于这个areaNo的 subgroup
            let groupSetx = JSON.parse(SPGet("groupSetx"));
            let groupSetItem = [];
            groupSetItem = groupSetx.filter((item) => {
                if (item.groupNo == areadate[1]) {
                    item.tem = temx
                    item.rgbH = 0
                    return item
                }
            })
            //meshLog('groupSetItem', groupSetItem);
            saveGroupSetInfor(groupSetItem)
            PubSub.publish('data', areadate[0])//返回给home界面
            PubSub.publish('temarea', areadate[1])//返回给area组内
            PubSub.publish('temneifanhui', 1)//判断色温是不是一样的，要是一样要返回给组
            PubSub.publish('RGBrgbHneifanhui', 1)//判断色温是不是一样的，要是一样要返回给组
        }
    }
}
//RGB界面中的RGBtiao的数据修改部分 async
export const SetRGBTiaodate = async (values) => {
    let rgbpanduan = JSON.parse(SPGet("rgbpanduan"))
    let areaSetx = JSON.parse(SPGet("areaSetx"));
    let areadate = PandanRGB(rgbpanduan)
    const token = SPGet('token')
    const email = SPGet('email')
    const meshUUID = SPGet('meshUUID')
    const addrs = areadate[1].substring(2, 4) + areadate[1].substring(0, 2);
    const opCode = "824D";
    let value1 = Math.ceil(values * 65535 / 100);
    const value = getOctets2Msg(value1);
    let result = await homeDeng(token, email, meshUUID, addrs, opCode, value);
    const dimx = Math.ceil((values) / 100 * 65535)
    const { ctl_status } = result;
    if (ctl_status == 0) {
        if (rgbpanduan == 1) {
            //修改localStorage上areaSetx的数据
            let areaitem;
            for (let index = 0; index < areaSetx.length; index++) {
                if (areaSetx[index].areaNo == areadate[1]) {
                    areaitem = areaSetx[index]
                }
            }
            //修改亮度的时候，在rgb状态下的hsl的值的也要修改的
            if (areaitem.rgbH > 0) {
                let hslh = areaitem.rgbH
                let hsls = areaitem.rgbS
                let hsll = areaitem.rgbL
                let hsl = HSLtoHSVtoHSL(hslh, hsls, hsll, dimx)
                areaitem.dim = dimx
                areaitem.rgbH = hsl[0]
                areaitem.rgbS = hsl[1]
                areaitem.rgbL = hsl[2]
                saveAreaInfor(areaitem)
            } else {
                areaitem.dim = dimx
                //meshLog('areaitem', areaitem);
                saveAreaInfor(areaitem)
            }
            //修改localStorage上deviceSetx的数据
            let deviceSetx = JSON.parse(SPGet("deviceSetx"));
            let devSetItem = [];
            //查询属于这个areaNo的 device
            devSetItem = deviceSetx.filter((item) => {
                if (item.areaNo == areadate[1] && item.isOnline == '01') {
                    if (item.rgbH > 0) {
                        let hslh = item.rgbH
                        let hsls = item.rgbS
                        let hsll = item.rgbL
                        let hsl = HSLtoHSVtoHSL(hslh, hsls, hsll, dimx)
                        item.dim = dimx
                        item.rgbH = hsl[0]
                        item.rgbS = hsl[1]
                        item.rgbL = hsl[2]
                    } else {
                        item.dim = dimx
                    }
                    return item
                }
            })
            // meshLog('devSetItem', devSetItem);
            saveLightInfor(devSetItem)
            //查询属于这个areaNo的 subgroup
            let groupSetx = JSON.parse(SPGet("groupSetx"));
            let groupSetItem = [];
            groupSetItem = groupSetx.filter((item) => {
                if (item.areaNo == areadate[1] && item.isOnline == '01') {
                    if (item.rgbH > 0) {
                        let hslh = item.rgbH
                        let hsls = item.rgbS
                        let hsll = item.rgbL
                        let hsl = HSLtoHSVtoHSL(hslh, hsls, hsll, dimx)
                        item.dim = dimx
                        item.rgbH = hsl[0]
                        item.rgbS = hsl[1]
                        item.rgbL = hsl[2]
                    } else {
                        item.dim = dimx
                    }
                    return item
                }
            })
            // meshLog('groupSetItem', groupSetItem);
            saveGroupSetInfor(groupSetItem)
            PubSub.publish('data', areaitem)// meshLog("传到home页面上需要展示右边数据的组", item);
            PubSub.publish('areadata', areaitem)//传到组的亮度那边
            PubSub.publish('RGBtiao', areaitem)
        } else if (rgbpanduan == 2) {
            //修改localStorage上deviceSetx的数据
            let deviceSetx = JSON.parse(SPGet("deviceSetx"));
            let devSetItem = [];
            //查询属于这个areaNo的 device
            devSetItem = deviceSetx.filter((item) => {
                if (item.address == areadate[1] && item.isOnline == '01') {
                    if (item.rgbH > 0) {
                        let hslh = item.rgbH
                        let hsls = item.rgbS
                        let hsll = item.rgbL
                        let hsl = HSLtoHSVtoHSL(hslh, hsls, hsll, dimx)
                        item.dim = dimx
                        item.rgbH = hsl[0]
                        item.rgbS = hsl[1]
                        item.rgbL = hsl[2]
                    } else {
                        item.dim = dimx
                    }
                    return item
                }
            })
            //('devSetItem', devSetItem);
            saveLightInfor(devSetItem)
            if (devSetItem[0].areaNo == 'C000' || devSetItem[0].areaNo == '0') {
                meshLog(' adminUtils.js 1002 灯在ungroup');
                PubSub.publish('ungrouprgbzhuangtai', areadate[0]) //修改ungroup背景和数据显示
            } else {
                PubSub.publish('data', devSetItem[0])// meshLog("传到home页面上需要展示右边数据的组", item);
                PubSub.publish('neifanhui', devSetItem[0])//判断亮度是不是一样的，要是一样要返回给组
            }
            PubSub.publish('RGBtiao', devSetItem[0])
        } else if (rgbpanduan == 3) {
            let groupSetx = JSON.parse(SPGet("groupSetx"));
            let groupSetItem = [];
            groupSetItem = groupSetx.filter((item) => {
                if (item.groupNo == areadate[1] && item.isOnline == '01') {
                    if (item.rgbH > 0) {
                        let hslh = item.rgbH
                        let hsls = item.rgbS
                        let hsll = item.rgbL
                        let hsl = HSLtoHSVtoHSL(hslh, hsls, hsll, dimx)
                        item.dim = dimx
                        item.rgbH = hsl[0]
                        item.rgbS = hsl[1]
                        item.rgbL = hsl[2]
                    } else {
                        item.dim = dimx
                    }
                    return item
                }
            })
            // meshLog('groupSetItem', groupSetItem);
            saveGroupSetInfor(groupSetItem)
            PubSub.publish('data', groupSetItem[0])// meshLog("传到home页面上需要展示右边数据的组", item);
            PubSub.publish('neifanhui', groupSetItem[0])//判断亮度是不是一样的，要是一样要返回给组
            PubSub.publish('RGBtiao', groupSetItem[0])
        }
    }
}
//判断CCT界面所需要的调光返回有返回值只的
export const AllTempRange = () => {
    let rgbpanduan = JSON.parse(SPGet("rgbpanduan"))
    let areaSetxxx = PandanRGB(rgbpanduan)
    let maxtemdata = []
    if (rgbpanduan == 1) {
        maxtemdata = allAreaGetTempRange(areaSetxxx[0])
        meshLog('adminUtils.js 1044,areaSetxxx', rgbpanduan);
    } else if (rgbpanduan == 2) {
        maxtemdata = allAreaGetTempRange(areaSetxxx[1].pid)
        meshLog(' adminUtils.js 1047 areaSetxxx', rgbpanduan);
    } else if (rgbpanduan == 3) {
        maxtemdata = allAreaGetTempRange(areaSetxxx[0])
        meshLog(' adminUtils.js 1050 areaSetxxx', rgbpanduan);
    } else {
        meshLog('adminUtils.js 1052 没有情况',);
    }
    return maxtemdata
}
